package de.blanky.madMonkeys.map;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Dialog_new_map extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private ViewFrame view;
	private Map_Builder_MainFrame mbm;
	private JTextField textName;
	private JTextField textWidth;
	private JTextField textHeigth;
	private JButton okButton;
	private JButton cancelButton;
	
	public Dialog_new_map(Map_Builder_MainFrame mbm, ViewFrame v) {
		setBounds(100, 100, 450, 300);
		view = v;
		this.mbm = mbm;
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(0, 1, 0, 10));
		{
			JPanel panel_Name = new JPanel();
			FlowLayout fl_panel_Name = (FlowLayout) panel_Name.getLayout();
			fl_panel_Name.setAlignment(FlowLayout.LEFT);
			contentPanel.add(panel_Name);
			{
				JLabel lblName = new JLabel("Name: ");
				panel_Name.add(lblName);
			}
			{
				textName = new JTextField();
				panel_Name.add(textName);
				textName.setColumns(10);
			}
		}
		{
			JPanel panel_width = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel_width.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			contentPanel.add(panel_width);
			{
				JLabel lblWidth = new JLabel("Width: ");
				panel_width.add(lblWidth);
			}
			{
				textWidth = new JTextField();
				textWidth.setColumns(10);
				panel_width.add(textWidth);
			}
		}
		{
			JPanel panel_Heigth = new JPanel();
			FlowLayout flowLayout = (FlowLayout) panel_Heigth.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			contentPanel.add(panel_Heigth);
			{
				JLabel lblHeigth = new JLabel("Height: ");
				panel_Heigth.add(lblHeigth);
			}
			{
				textHeigth = new JTextField();
				textHeigth.setColumns(10);
				panel_Heigth.add(textHeigth);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(this);
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == okButton){
			mbm.setView(new ViewFrame(textName.getText(), Integer.parseInt(textHeigth.getText()), Integer.parseInt(textWidth.getText())));
			System.out.println("created new view with: "+textName.getText()+", "+Integer.parseInt(textWidth.getText())+", "+Integer.parseInt(textHeigth.getText()) );
			dispose();
		}
		if(e.getSource() == cancelButton){
			dispose();
		}
		
	}

}
